package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class YeJin {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "_yttoken_=y1ow2sxb7witkdty";
    public static final String ORIGIN = "http://mp.codeup.cn";
    public static final String REFERER = "http://mp.codeup.cn/store.html?shopId=181&pageCode=2587dc48-a952-4760-976b-01a9bca5c6f6&sh=sh&vt_=1582510127631";
    public static final String URL_PRE = "http://mp.codeup.cn";

    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        String url = "http://mp.codeup.cn/shoppagemoduleservice/queryModuleListByH5.do";
        Map<String, Object> parameter = MapUtil.newHashMap(2);
        //a630ce1a-b9df-464d-a706-f5c86391a7bd
        parameter.put("pageCode", "b1c75ddb-35c1-4736-84c8-ab9bced84f6b");
        parameter.put("shopId", 181);
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("ORIGIN", ORIGIN);
        headers.put("User-Agent", UserAgentUtil.random());
        headers.put("Referer", REFERER);
        String result = HttpUtil.doPost(url, headers, parameter);
        if (StrUtil.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            Boolean errCode = jsonObject.getBoolean("success");
            if (errCode != null && errCode == true) {
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray listObject = data.getJSONArray("pageModuleDTOList");
                int size = listObject.size();
                for (int k = 0; k < size; k++) {
                    JSONObject jsonTmp = listObject.getJSONObject(k);
                    String actionContent = jsonTmp.getString("actionContent");
                    if (actionContent != null) {
                        getBookList(actionContent);
                    }
                }
            }
        }
    }

    public void getBookList(String code) throws Exception {
        String url = "http://mp.codeup.cn/shoppagemoduleservice/queryModuleListByH5.do";
        Map<String, Object> parameter = MapUtil.newHashMap(2);
        parameter.put("pageCode", code);
        parameter.put("shopId", 181);
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        headers.put("Referer", REFERER);
        String result = HttpUtil.doPost(url, headers, parameter);
        String digUri = "http://mp.codeup.cn/book/ebook.htm?bookId=";
        if (StrUtil.isNotBlank(result)) {
            JSONObject jsonObject = JSONObject.parseObject(result);
            Boolean errCode = jsonObject.getBoolean("success");
            if (errCode != null && errCode == true) {
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray listObject = data.getJSONArray("pageModuleDTOList");
                int size = listObject.size();
                for (int k = 0; k < size; k++) {
                    JSONObject jsonTmp = listObject.getJSONObject(k);
                    JSONArray bookList = jsonTmp.getJSONArray("bookDTOList");
                    if (bookList!=null && bookList.size() > 0) {
                        List<CxgBook> listBook = new ArrayList<>();
                        for (int m = 0; m < bookList.size(); m++) {
                            CxgBook bookEntity = new CxgBook();
                            JSONObject bookTpm = bookList.getJSONObject(m);
                            bookEntity.setDigitalUri(digUri + bookTpm.getString("id") + "&srcchannel=mp");
                            if (bookTpm.getLong("onlineTime") != null) {
                                LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(bookTpm.getLong("onlineTime") / 1000, 0, ZoneOffset.ofHours(8));
                                bookEntity.setPubDate(localDateTime);
                            }
                            bookEntity.setBookIsbn(bookTpm.getString("isbn"));
                            bookEntity.setPkId(bookTpm.getString("id"));
                            bookEntity.setAuthor(bookTpm.getString("author"));
                            bookEntity.setBookName(bookTpm.getString("name"));
                            bookEntity.setBrief(bookTpm.getString("description"));
                            bookEntity.setCover(bookTpm.getString("thumbnails"));
                            bookEntity.setIsSync(0);
                            bookEntity.setCreateTime(LocalDateTime.now());
                            bookEntity.setPressName("冶金工业出版社");
                            bookEntity.setStatus(1);
                            bookEntity.setPressId(60);
                            listBook.add(bookEntity);
                        }
                        bookService.saveBatch(listBook);
                    }
                }
            }
        }
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
